﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NTQOnlineExam.Services.DBCommand.Examination;
using NTQOnlineExam.Services.Infrastructure;
using NTQOnlineExam.Services.Models;

namespace NTQOnlineExam.Services.ReportService.Answer
{
    public class AnswerReportService : IAnswerReportService
    {
        private readonly ICommandExecutor _executor;

        public AnswerReportService(ICommandExecutor executor)
        {
            _executor = executor;
        }

        public IList<AnswerDTO> GetAnswers(int questionId)
        {
            var answerTable = DBCommand.DBCommand.CreateInstant().GetAnswerDbCommand(questionId);
            var result = ConvertDataTableToAnswerDtos(answerTable);
            return result;
        }

        public IList<AnswerDTO> GetAnswerForQuestions(IList<int> questionIds)
        {
            return _executor.Execute(new GetAnswerByQuestionListDbCommand(questionIds));
        }

        private IList<AnswerDTO> ConvertDataTableToAnswerDtos(DataTable dtTable)
        {
            var answerDTOs = new List<AnswerDTO>();
            foreach (DataRow dtRow in dtTable.Rows)
            {
                var answerDTO = new AnswerDTO();
                answerDTO.QuestionId = (int)dtRow["QuestionId"];
                answerDTO.AnswerId = (int)dtRow["AnswerId"];
                answerDTO.AnswerContent = dtRow["Content"].ToString();
                answerDTOs.Add(answerDTO);
            }
            return answerDTOs;
        }
    }
}
